package com.base.test.dao.shardingtype.db;



import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.util.Collection;
/**
 * @title 自定义分库策略
 * @author carl
 * @createDate 2019-11-22
 */
public class PreciseLongModuloShardingDatabaseAlgorithm implements PreciseShardingAlgorithm<Long> {
    
    @Override
    public String doSharding(final Collection<String> databaseNames, final PreciseShardingValue<Long> shardingValue) {
        int size = databaseNames.size();
        for (String each : databaseNames) {
            if (each.endsWith(shardingValue.getValue() % size + "")) {
                return each;
            }
        }
        throw new UnsupportedOperationException();
    }
}
